/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.hugo.senai.classes;


import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;

/**
 *
 * @author alunoo
 */
public class EnviarEmail {

    public void enviar(final String remetente, final String senha, final String destinatario)  {

        //RECUPERAR SENHA
        Properties props = new Properties();
        /**
         * Parâmetros de conexão com servidor Gmail
         */
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        //   props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.debug.auth", "true");

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(remetente, senha);
                    }
                });

        /**
         * Ativa Debug para sessão
         */
        session.setDebug(true);

       

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(remetente)); //Remetente

            Address[] toUser = InternetAddress //Destinatário(s)
                    .parse(destinatario);

            message.setRecipients(Message.RecipientType.TO, toUser);
            message.setSubject("Enviando email com JavaMail");//Assunto
            message.setText("Enviei este email utilizando JavaMail com minha conta GMail!");
            /**
             * Método para enviar a mensagem criada
             */
            Transport.send(message);

            JOptionPane.showMessageDialog(null, "Email enviado!\n Verifique o seu email.");

        } catch (MessagingException e) {

    Properties props1 = new Properties();

        props1.put("mail.transport.protocol", "smtp");
        props1.put("mail.smtp.host", "smtp.gmail.com");
        props1.put("mail.smtp.socketFactory.port", "587");
        props1.put("mail.smtp.socketFactory.fallback", "false");
        props1.put("mail.smtp.starttls.enable", "true");
        props1.put("mail.smtp.auth", "true");
        props1.put("mail.smtp.port", "587");
        props1.put("mail.debug", "true");
        props1.put("mail.debug.auth", "true");
        props1.put("mail.smtp.ssl.trust", "smtp.gmail.com");

        Session session2 = Session.getDefaultInstance(props1,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(remetente, senha);
                    }
                });
            try {

                Message message1 = new MimeMessage(session2);
                message1.setFrom(new InternetAddress(remetente)); //Remetente

                Address[] toUser = InternetAddress //Destinatário(s)
                        .parse(destinatario);

                message1.setRecipients(Message.RecipientType.TO, toUser);
                message1.setSubject("Enviando email com JavaMail");//Assunto
                message1.setText("Enviei este email utilizando JavaMail com minha conta GMail!");
                /**
                 * Método para enviar a mensagem criada
                 */
                Transport.send(message1);

                JOptionPane.showMessageDialog(null, "Email enviado!\n Verifique o seu email.");
                        
            } catch (MessagingException e1) {

                JOptionPane.showMessageDialog(null, "Erro ao enviar email. Verifique o firewall.\n" + e1.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);

            }

        }

    }
    
    
    public void enviar2(String destinatario){
        
        
        final String username = "hugohbpereira@gmail.com";
        final String password = "hajava10naveia";

        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "587");
       // props.put("mail.smtp.socketFactory.fallback", "false");
        
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        props.put("mail.debug", "true");
        props.put("mail.debug.auth", "true");
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
         //props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        //props.put( "mail.smtp.quitwait", "false" ); 

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });


        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("hugohppereira@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("hubliroldis@hotmail.com"));
            message.setSubject("MENSAGEM ANÔNIMA");
            message.setText("CARO HUGO,"
                    + "\n\n VOCÊ ESTÁ SENDO VÍTIMA DE ATAQUES VIRTUAIS!!!!!!!!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            JOptionPane.showMessageDialog(null, "Erro ao enviar email!!", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    }

}
